package com.wannengbang.storemobile.homepage;

import androidx.appcompat.app.AppCompatActivity;

import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.wannengbang.storemobile.R;
import com.wannengbang.storemobile.base.BaseActivity;
import com.wannengbang.storemobile.base.DataCallBack;
import com.wannengbang.storemobile.bean.GetWxMercIdBean;
import com.wannengbang.storemobile.bean.MerchantDetailsBean;
import com.wannengbang.storemobile.bean.SignContractBean;
import com.wannengbang.storemobile.bean.StoreDeatilsBean;
import com.wannengbang.storemobile.event.StoreDataEvent;
import com.wannengbang.storemobile.homepage.model.HomePageModelImpl;
import com.wannengbang.storemobile.homepage.model.IHomePageModel;
import com.wannengbang.storemobile.mine.UserAgreementActivity;
import com.wannengbang.storemobile.utils.DateTimeUtil;
import com.wannengbang.storemobile.utils.ToastUtil;
import com.wannengbang.storemobile.widget.AppTitleBar;

import org.greenrobot.eventbus.EventBus;

import java.util.Date;

import butterknife.BindView;
import butterknife.ButterKnife;

public class RegistrationInformationActivity extends BaseActivity {

    @BindView(R.id.title_bar)
    AppTitleBar titleBar;
    @BindView(R.id.tv_status)
    TextView tv_status;
    @BindView(R.id.tv_time)
    TextView tv_time;
    @BindView(R.id.tv_name)
    TextView tv_name;
    @BindView(R.id.tv_store_no)
    TextView tv_store_no;
    @BindView(R.id.tv_ys_merc_id)
    TextView tv_ys_merc_id;
    @BindView(R.id.tv_yinsheng_sys_flowId)
    TextView tv_yinsheng_sys_flowId;
    @BindView(R.id.tv_sign_status)
    TextView tv_sign_status;
    @BindView(R.id.tv_feedback)
    TextView tv_feedback;
    @BindView(R.id.tv_sign_contract)
    TextView tv_sign_contract;
    @BindView(R.id.tv_information)
    TextView tv_information;
    @BindView(R.id.tv_rate)
    TextView tv_rate;
    @BindView(R.id.tv_wechat_official_account)
    TextView tv_wechat_official_account;
    @BindView(R.id.tv_wechat_merchant_number)
    TextView tv_wechat_merchant_number;


    private IHomePageModel homePageModel = new HomePageModelImpl();
    private int id;
    private String store_no;
    private String ys_merc_id;
    private String yinsheng_sys_flowId;
    private String qianyue_url;
    private int sign_status;
    private int pay_channel_status;
    private int is_cs;
    private String thirdMercId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_registration_information);
        ButterKnife.bind(this);
        initView();
        initData();
        initEvent();

    }

    public void initView() {
        Intent intent = getIntent();
        id = intent.getIntExtra("id", 0);
    }

    public void initData() {
        homePageModel.requestStoreShow(String.valueOf(id), new DataCallBack<MerchantDetailsBean>() {
            @Override
            public void onSuccessful(MerchantDetailsBean bean) {
                store_no = bean.getData().getStore_no();
                int rate = bean.getData().getRate();
                ys_merc_id = bean.getData().getYs_merc_id();
                yinsheng_sys_flowId = bean.getData().getYinsheng_sys_flowId();
                tv_time.setText(DateTimeUtil.format7(new Date(Long.parseLong(String.valueOf(bean.getData().getCreate_time())) * 1000)));
                tv_name.setText(bean.getData().getName());
                tv_rate.setText("费率：" + rate);
                tv_store_no.setText("门店编号：" + store_no);
                tv_ys_merc_id.setText("商户编号：" + ys_merc_id);
                tv_yinsheng_sys_flowId.setText("入网流水号：" + yinsheng_sys_flowId);
                tv_feedback.setText("审核反馈：" + bean.getData().getReject_msg());
                pay_channel_status = bean.getData().getPay_channel_status();
                sign_status = bean.getData().getSign_status();
                qianyue_url = bean.getData().getQianyue_url();
                is_cs = bean.getData().getIs_cs();
                //审核状态
                if (pay_channel_status == 0) {
                    tv_status.setText("待提交资料");
                } else if (pay_channel_status == 1) {
                    tv_status.setText("等待审核");
                } else if (pay_channel_status == 2) {
                    tv_status.setText("正在审核");
                } else if (pay_channel_status == 3) {
                    tv_status.setText("已通过");
                } else if (pay_channel_status == 4) {
                    tv_feedback.setVisibility(View.GONE);
                    tv_status.setText("已驳回");
                    tv_information.setText("修改资料");
                } else if (pay_channel_status == 5) {
                    tv_status.setText("正在审核");
                } else {
                    tv_status.setText("未知");
                }

                //签约状态 0-申请中 1-待签署 2-已创建（成功最终状态） 3-已作废 4-已过期 7-创建失败 8-合同已生成
                if (sign_status == 0) {
                    tv_sign_status.setText("签约状态：申请中");
                } else if (sign_status == 1) {
                    tv_sign_status.setText("签约状态：待签署");
                } else if (sign_status == 2) {
                    tv_sign_status.setText("签约状态：已创建");
                } else if (sign_status == 3) {
                    tv_sign_status.setText("签约状态：已作废");
                } else if (sign_status == 4) {
                    tv_sign_status.setText("签约状态：已过期");
                } else if (sign_status == 7) {
                    tv_sign_status.setText("签约状态：创建失败");
                } else if (sign_status == 8) {
                    tv_sign_status.setText("签约状态：合同已生成");
                } else {
                    tv_sign_status.setText("签约状态：未知");
                }

                if (pay_channel_status == 1 && (sign_status == 0 || sign_status == 1 | sign_status == 8)) {
                    tv_sign_contract.setText("签约");
                    tv_sign_contract.setVisibility(View.VISIBLE);
                } else if (is_cs == 1 && pay_channel_status == 1 && (sign_status == 0 || sign_status == 4 || null == qianyue_url)) {
                    tv_sign_contract.setText("重新签约");
                    tv_sign_contract.setVisibility(View.VISIBLE);
                } else if (pay_channel_status == 3 && sign_status == 2) {
//                    tv_sign_contract.setText("下载合同");
//                    tv_sign_contract.setVisibility(View.VISIBLE);
                }

                if (bean.getData().getReject_msg().isEmpty()) {
                    tv_feedback.setVisibility(View.GONE);
                } else {
                    tv_feedback.setVisibility(View.VISIBLE);
                }

            }

            @Override
            public void onFailed(String msg, String code) {

            }
        });
    }

    public void initEvent() {
        //门店编号
        tv_store_no.setOnClickListener(v -> {
            //获取剪贴板管理器
            ClipboardManager cm = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
            // 创建普通字符型ClipData
            ClipData mClipData = ClipData.newPlainText("Label", store_no);
            // 将ClipData内容放到系统剪贴板里。
            cm.setPrimaryClip(mClipData);
            ToastUtil.showShort("复制成功");
        });
        //商户编号
        tv_ys_merc_id.setOnClickListener(v -> {
            //获取剪贴板管理器
            ClipboardManager cm = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
            // 创建普通字符型ClipData
            ClipData mClipData = ClipData.newPlainText("Label", ys_merc_id);
            // 将ClipData内容放到系统剪贴板里。
            cm.setPrimaryClip(mClipData);
            ToastUtil.showShort("复制成功");
        });
        //入网流水号
        tv_yinsheng_sys_flowId.setOnClickListener(v -> {
            //获取剪贴板管理器
            ClipboardManager cm = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
            // 创建普通字符型ClipData
            ClipData mClipData = ClipData.newPlainText("Label", yinsheng_sys_flowId);
            // 将ClipData内容放到系统剪贴板里。
            cm.setPrimaryClip(mClipData);
            ToastUtil.showShort("复制成功");
        });

        tv_sign_contract.setOnClickListener(v -> {
            if (pay_channel_status == 1 && (sign_status == 0 || sign_status == 1 | sign_status == 8)) {
                Intent intent = new Intent(RegistrationInformationActivity.this, UserAgreementActivity.class);
                intent.putExtra("type", 5);
                intent.putExtra("url", qianyue_url);
                startActivity(intent);

            } else if (is_cs == 1 && pay_channel_status == 1 && (sign_status == 0 || sign_status == 4 || null == qianyue_url)) {
                sendSignSms(store_no);
            } else if (pay_channel_status == 3 && sign_status == 2) {
                //跳转自带浏览器下载
                Uri uri = Uri.parse(qianyue_url);
                Intent intent = new Intent(Intent.ACTION_VIEW, uri);
                startActivity(intent);
            }
        });

        tv_information.setOnClickListener(v -> {


            new HomePageModelImpl().requestStoreProfile(new DataCallBack<StoreDeatilsBean>() {
                @Override
                public void onSuccessful(StoreDeatilsBean bean) {
                    if (bean.getData() != null) {
                        EventBus.getDefault().postSticky(new StoreDataEvent(bean.getData()));
                        if (sign_status == 4) {
                            Intent intent = new Intent(RegistrationInformationActivity.this, StoreDataActivity.class);
                            intent.putExtra("isShow", "1");
                            startActivity(intent);
                        } else {
                            Intent intent = new Intent(RegistrationInformationActivity.this, StoreDataActivity.class);
                            intent.putExtra("isShow", "0");
                            startActivity(intent);
                        }
                    }
                }

                @Override
                public void onFailed(String msg, String code) {

                }
            });


        });


        tv_wechat_official_account.setOnClickListener(v -> {

            new HomePageModelImpl().requestGetWxMercId(store_no, ys_merc_id, new DataCallBack<GetWxMercIdBean>() {
                @Override
                public void onSuccessful(GetWxMercIdBean bean) {
                    if (bean.getData() != null) {
                        thirdMercId = bean.getData().getThirdMercId();
                        tv_wechat_merchant_number.setText("微信商户号：" + thirdMercId);

                        if (TextUtils.isEmpty(thirdMercId)) {
                            tv_wechat_merchant_number.setVisibility(View.GONE);
                            tv_wechat_official_account.setVisibility(View.VISIBLE);
                        } else {
                            tv_wechat_merchant_number.setVisibility(View.VISIBLE);
                            tv_wechat_official_account.setVisibility(View.GONE);
                        }
                    }
                }

                @Override
                public void onFailed(String msg, String code) {

                }
            });
        });

        tv_wechat_merchant_number.setOnClickListener(v -> {
            //获取剪贴板管理器
            ClipboardManager cm = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
            // 创建普通字符型ClipData
            ClipData mClipData = ClipData.newPlainText("Label", thirdMercId);
            // 将ClipData内容放到系统剪贴板里。
            cm.setPrimaryClip(mClipData);
            ToastUtil.showShort("复制成功");

        });

    }

    private void sendSignSms(String store_no) {

        homePageModel.sendSignSms(store_no, new DataCallBack<SignContractBean>() {
            @Override
            public void onSuccessful(SignContractBean bean) {
                String qianyue_url = bean.getData().getQianyue_url();
                Intent intent = new Intent(RegistrationInformationActivity.this, UserAgreementActivity.class);
                intent.putExtra("type", 5);
                intent.putExtra("url", qianyue_url);
                startActivity(intent);
            }

            @Override
            public void onFailed(String msg, String code) {

            }
        });
    }
}